package simpleTaskBoard.business;

import java.util.ArrayList;
import java.util.List;

import ch.ivyteam.ivy.environment.Ivy;
import ch.ivyteam.ivy.scripting.objects.DateTime;

import simpleTaskBoard.dao.HistoryDAO;
import simpleTaskBoard.dto.HistoryDTO;
import simpleTaskBoard.dto.TaskDTO;
import simpleTaskBoard.entities.HistoryEntity;
import simpleTaskBoard.enums.ActionChangeEnum;


public class HistoryBusiness {
	public HistoryBusiness(){
		
	};
	
	public List<HistoryDTO> findHistoryByTask(Long taskID){
		List<HistoryDTO> listHistory = new ArrayList<HistoryDTO>();
		HistoryDAO dao = new HistoryDAO();
		List<HistoryEntity> listHistoryDao = dao.findHistoryByTask(taskID);
		for(HistoryEntity h : listHistoryDao){
			listHistory.add(new HistoryDTO(h));
		}
		return listHistory;
	}
	
	public void addHistory(HistoryDTO history){
		HistoryDAO dao = new HistoryDAO();
		HistoryEntity hisEntity= history.convertToEntity(history);
		dao.addHistory(hisEntity);
	}
	
	public void setChangeHistory(TaskDTO newTask,TaskDTO oldTask){
		
		if(!newTask.getTitle().equalsIgnoreCase(oldTask.getTitle())){
			setChangeTitle(newTask,oldTask);
		}

		if(!newTask.getDescription().equalsIgnoreCase(oldTask.getDescription())){
			setChangeDescription(newTask,oldTask);
		
		}
		
		if((oldTask.getAssigned()==null && newTask.getAssigned().trim()!="") ||
				(oldTask.getAssigned()!=null && !newTask.getAssigned().equalsIgnoreCase(oldTask.getAssigned()))
				){
			setChangeAssignee(newTask,oldTask);
		}
	}
	
	private void setChangeAssignee(TaskDTO newTask, TaskDTO oldTask) {
		HistoryDAO historyDAO= new HistoryDAO();
		HistoryDTO historyDTO= new HistoryDTO();
		historyDTO.setUserChange(Ivy.session().getSessionUserName());
		historyDTO.setDateChange(new DateTime());
		
		historyDTO.setFromChange(oldTask.getAssigned());
		historyDTO.setToChange(newTask.getAssigned());
		historyDTO.setActionChange(ActionChangeEnum.ASSIGN);
		historyDTO.setTask(oldTask);
		historyDAO.addHistory(historyDTO.convertToEntity(historyDTO));
	}

	private void setChangeDescription(TaskDTO newTask, TaskDTO oldTask) {
		HistoryDAO historyDAO= new HistoryDAO();
		HistoryDTO historyDTO= new HistoryDTO();
		historyDTO.setUserChange(Ivy.session().getSessionUserName());
		historyDTO.setDateChange(new DateTime());
		
		historyDTO.setFromChange(oldTask.getDescription());
		historyDTO.setToChange(newTask.getDescription());
		historyDTO.setActionChange(ActionChangeEnum.DESCRIPTION);
		historyDTO.setTask(oldTask);
		historyDAO.addHistory(historyDTO.convertToEntity(historyDTO));
	}

	private void setChangeTitle(TaskDTO newTask, TaskDTO oldTask) {
		HistoryDAO historyDAO= new HistoryDAO();
		HistoryDTO historyDTO= new HistoryDTO();
		historyDTO.setUserChange(Ivy.session().getSessionUserName());
		historyDTO.setDateChange(new DateTime());
		
		historyDTO.setFromChange(oldTask.getTitle());
		historyDTO.setToChange(newTask.getTitle());
		historyDTO.setActionChange(ActionChangeEnum.TITLE);
		historyDTO.setTask(oldTask);
		historyDAO.addHistory(historyDTO.convertToEntity(historyDTO));
	}
	
	public HistoryDTO findLastLogWorkByTask(Long taskID){
		HistoryDAO dao = new HistoryDAO();
		return new HistoryDTO(dao.findLastLogWorkByTask(taskID));
	}
}
